import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class Project {
    public static void main(String[] args){
        ShowPow showPow = new ShowPow();
        SumCode sumCode = new SumCode();
    }
}

class ShowPow extends JFrame{
    static JTextField text1,text2;
    static JTextArea textArea;
    JScrollPane jsp;
    DealSig dealSig = new DealSig();
    ShowPow(){
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        jsp = new JScrollPane(textArea);
        text1 =new JTextField(10);
        text1.setHorizontalAlignment(JTextField.LEFT);
        text2 =new JTextField(10);
        text1.addActionListener(dealSig);
        Container con = getContentPane();
        con.setLayout(new GridLayout(2,1,10,10));
        con.add(text1);
        con.add(text2);
        con.add(jsp);
        setSize(500,300);
        setVisible(true);
    }
}

class DealSig implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==ShowPow.text1){
            try {
                long ret = Long.parseLong(ShowPow.text1.getText());
                ShowPow.text2.setText(String.valueOf(ret*ret));
                String log = ShowPow.textArea.getText();
                log += new Date().toString();
                log+=(":"+String.valueOf(ret*ret)+"\r\n");
                ShowPow.textArea.setText(log);

            }catch (Exception exception){
                ShowPow.text2.setText("请输入整数");
            }
        }
    }
}

/*
求和计算器
    1.创建输入框，两个按键，构成输入部分。按键当独放到一个面板，此面板和输入框放到一个大面板布局。
    2.创建一个文本区，和1形成的大面板布局，提交到主面板显示。
    3.为按键添加动作响应。
 */
class SumCode extends JFrame implements ActionListener{
    JTextField textField;
    JTextArea textArea;
    JButton sumBt,clearBt;

    SumCode(){
        textField = new JTextField();
        textArea = new JTextArea();
        //textField.setPreferredSize(new Dimension(400,100));//固定文本框大小
         //textField.setSize(500,100);
        textField.setHorizontalAlignment(JTextField.LEFT);//指定文本框从左边开始显示

        sumBt = new JButton("求和");
        //sumBt.setBounds(0,0,90,40);
        clearBt = new JButton("清除");
        //clearBt.setBounds(0,60,90,40);
        sumBt.addActionListener(this);
        clearBt.addActionListener(this);

        JPanel btPanel = new JPanel();
        btPanel.setPreferredSize(new Dimension(100,100));
        btPanel.add(sumBt);
        btPanel.add(clearBt);
        JPanel inPanel = new JPanel(new GridLayout(2,1));
        inPanel.setSize(500,100);
        inPanel.add(textField);
        inPanel.add(btPanel);


        setSize(500,400);
        Container con = getContentPane();
        con.setLayout(new GridLayout(2,1));
        con.add(inPanel);
        con.add(textArea);
        setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==sumBt){
            String str = textField.getText();
            String rev[] = str.split(" ");
            long sum = 0;
            int len = rev.length;
            if(len>0) {
                for (int i = 0; i < len; i++) {
                    try {
                        sum += Long.parseLong(rev[i]);
                    }catch (Exception ex){
                    }
                }
            }
            else{
                try {
                    sum += Long.parseLong(str);
                }catch (Exception ex){
                    System.out.println("2:"+ex);
                }
            }
            String log = textArea.getText();
            log += new Date().toString();
            log+=(":"+ sum +"\r\n");
            textArea.setText(log);
        }else if (e.getSource()==clearBt){
            textArea.setText("");
            textField.setText("");
        }else{

        }
    }
}
